'''
Created on Apr 13, 2012

@author: Johnathon
'''
from LineValues import One, Zero, X
import Optimizer
import sys
import unittest


class Test(unittest.TestCase):


    def test1(self):
        values = [1, 3, 5, 7, 9, 0, 2, 4, 6, 8]
        comparator = lambda x,y: abs(x-y)
        o = Optimizer.optimize(values, comparator)
#        print o
        self.assertTrue(o == [8,7,6,5,4,3,2,1,0,9])

    def test2(self):
        values = [1, 3, 5, 7, 9, 0, 2, 4, 6, 8]
        comparator = lambda x,y: abs(x-y)
        o = Optimizer.optimize2(values, comparator)
#        print o
        values.sort()
#        print values
        self.assertTrue(o == values)
        
#    def test3(self):
#        values = [(1,), (2, 3), (1000, 1001, 1002)]
#        comparator = self.minBetweenTuples
#        o = Optimizer.optimize3(values, comparator, (None, None, None, None))
##        print o
#        self.assertTrue(o == [1000, 2, 1])
    
    def testHammingDistance(self):
        ones = [One,One,One,One,One,One]
        zeroes = [Zero,Zero,Zero,Zero,Zero,Zero]
        test = Optimizer.hammingDistanceComparator(ones, [zeroes])
        self.assertTrue(test[0] == 6)
        self.assertTrue(test[1] == ones)
        self.assertTrue(test[2] == zeroes)
        
        x = [One, Zero, One, Zero, X, X]
        test = Optimizer.hammingDistanceComparator(ones, [x])
        self.assertTrue(test[0] == 2)
        self.assertTrue(test[2] == [One, Zero, One, Zero, One, One])
        
        test = Optimizer.hammingDistanceComparator(ones, [zeroes, x, ones])
        self.assertTrue(test[0] == 0)
        self.assertTrue(test[1] == ones)
        self.assertTrue(test[2] == ones)
        
    
    def testXSetter(self):
        ones = [One,One,One,One,One,One]
        zeroes = [Zero,Zero,Zero,Zero,Zero,Zero]
        test = Optimizer.XSetter(ones, zeroes)
        self.assertTrue(test[0] == ones)
        self.assertTrue(test[1] == zeroes)
        
        test = Optimizer.XSetter(ones, [X, X, X, X, X, X])
        self.assertTrue(test[1] == ones)
        test = Optimizer.XSetter(zeroes, [X, X, X, X, X, X])
        self.assertTrue(test[1] == zeroes)
        
        test = Optimizer.XSetter([X, X, X, X, X, X], [X, X, X, X, X, X], True)
        self.assertTrue(test[1] == ones)


    def minBetweenTuples(self, x, ys):
        minDist = sys.maxint
        for y in ys:
            dist = abs(x-y)
            if dist < minDist:
                minX = x
                minY = y
                minDist = dist
        return (minDist, minX, minY)
                    

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()